﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Drawing;

namespace Jemgine.MapEditor
{
    [Tools.SelectableTool]
    public class AddPointTool : Tool
    {
        public override Image GetIcon() { return System.Drawing.Image.FromFile("ToolIcons/addpoint.bmp"); }

        public override void MouseDown(MouseInputState InputState, EditorState EditorState)
        {
            var TopObject = EditorState.GetTopObject(EditorState.Selected) as Data.EntityRecord;
            if (TopObject == null) return;

            var MiddleObject = EditorState.GetMiddleObject(EditorState.Selected);

            Vector2 LocalPosition = TopObject.TransformToLocalSpace(InputState.Position);
            bool Snap = (InputState.Modifiers & System.Windows.Forms.Keys.Alt) == System.Windows.Forms.Keys.Alt;
            if (Snap) LocalPosition = EditorState.SnapToGrid(LocalPosition);

            if (MiddleObject is Data.EditorPolygon)
                EditorState.PerformOperation(new Operations.InsertPoint(EditorState.Selected, LocalPosition));
            else if (MiddleObject is Data.Line && EditorState.Selected.Type == Engine.Selection.Types.Vertex)
            {
                var Line = MiddleObject as Jemgine.Data.Line;
                Vector2 EndA = Line[EditorState.Selected.BottomIndex].Position;
                EditorState.PerformOperation(new Operations.NewLine(Line, EditorState.Selected, EndA, LocalPosition));
            }
        }
    }
}
